package org.pmt.first.kms.screen

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import androidx.navigation.toRoute
import com.dream.permission.PermissionAction
import com.dream.permission.rememberPermissionPresenter

import com.dream.ui.foundation.animation.AniMotionScheme
import com.dream.ui.foundation.animation.LocalAniMotionScheme
import com.dream.ui.foundation.animation.LocalNavigationMotionScheme
import com.dream.ui.foundation.animation.NavigationMotionScheme
import com.dream.ui.foundation.widgets.BackNavigationIconButton
import dev.icerock.moko.permissions.Permission
import dev.icerock.moko.permissions.camera.CAMERA
import dev.icerock.moko.permissions.contacts.CONTACTS
import dev.icerock.moko.permissions.motion.MOTION
import dev.icerock.moko.permissions.storage.STORAGE
import dev.icerock.moko.permissions.storage.WRITE_STORAGE
import io.github.aakira.napier.Napier
import org.pmt.first.kms.navigation.BottomNavItem
import org.pmt.first.resource.NavRoutes

/**
 * Author: pengmutian
 * Date: 2025/2/24 15:12
 * Description: DemoScreen
 */

@Composable
fun DetailScreen(navController: NavHostController, itemId: String) {
    // 显示传递过来的参数
    Column {
        Text("Item ID: $itemId")
        Button(
            onClick = {
                navController.navigate(NavRoutes.SETTING)
            }
        ) {
            Text("Go to home")
        }
    }
}

@Composable
fun CustomNavigationBar(
    navController: NavHostController,
    modifier: Modifier = Modifier,
    items: List<BottomNavItem>,
) {
    // 获取当前导航栈中的当前路由
    val navBackStackEntry by navController.currentBackStackEntryAsState()
    // FIXME 目前只有主页有选中状态，其他页面没有，如果处理还是思考中
    val currentRoute = navBackStackEntry?.toRoute<NavRoutes.HOME>()

    NavigationBar(
        modifier = modifier
    ) {
        items.forEach { item ->
            NavigationBarItem(
                icon = {
                    Icon(
                        imageVector = if (currentRoute == item.route) item.selectedIcon else item.unselectedIcon,
                        contentDescription = item.title
                    )
                },
                label = { Text(text = item.title) },
                selected = currentRoute == item.route,
                onClick = {
                    // 导航到指定路由
                    navController.navigate(item.route) {
                        // 避免重复添加相同的目的地到栈中
                        navController.graph.startDestinationRoute?.let { route ->
                            popUpTo(route) {
                                saveState = true
                            }
                        }
                        // 避免重复导航到相同的目的地
                        launchSingleTop = true
                        // 恢复之前保存的状态
                        restoreState = true
                    }
                }
            )
        }
    }
}

@Composable
fun ProvideAniMotionCompositionLocals(
    content: @Composable () -> Unit
) {
    val density by rememberUpdatedState(LocalDensity.current)
    val navigationMotionScheme by remember {
        derivedStateOf {
            NavigationMotionScheme.calculate(useSlide = false)
        }
    }
    val aniMotionScheme by remember {
        derivedStateOf {
            AniMotionScheme.calculate(density)
        }
    }
    CompositionLocalProvider(
        LocalNavigationMotionScheme provides navigationMotionScheme,
        LocalAniMotionScheme provides aniMotionScheme,
        content = content,
    )
}

@Composable
fun AppScreen() {
    ProvideAniMotionCompositionLocals({ content() })
}

@Composable
fun content(){
    val navController: NavHostController = rememberNavController()
    val items = listOf(BottomNavItem.FOR_YOU, BottomNavItem.BOOKMARKS, BottomNavItem.INTERESTS)
//    val (state, channel) = rememberPermissionPresenter()
    Surface(
        modifier = Modifier.fillMaxSize(),
        color = MaterialTheme.colorScheme.background
    ) {
        Scaffold(
            bottomBar = {
                CustomNavigationBar(
                    navController = navController,
                    modifier = Modifier.padding(0.dp),
                    items = items
                )
            }
        ) { paddingValues ->

            Column(modifier = Modifier.padding(paddingValues)) {
                NavHost(
                    navController = navController,
                    startDestination = NavRoutes.HOME
                ) {
                    composable<NavRoutes.HOME> {
                        HomeNewScreen(navController)
                    }
                    composable<NavRoutes.DETAIL> { backStackEntry ->
                        val itemId = backStackEntry.arguments?.getString("itemId")
                        if (itemId != null) {
                            DetailScreen(navController, itemId) // 恢复并修正代码
                        } else {
                            // 处理 itemId 为 null 的情况
                            Text("Invalid item ID")
                        }
//                        channel.trySend(PermissionAction(Permission.CONTACTS))
                    }
                    composable<NavRoutes.SETTING> {
                        SettingScreen(navController)

                        SettingsScreen(
                            viewModel {
                                SettingsViewModel()
                            },
                            Modifier.fillMaxSize(),
                            navigationIcon = {
                                BackNavigationIconButton(
                                    {
                                        navController.popBackStack()
                                    },
                                )
                            },
                        )
                    }
                }
            }
        }
    }
}

